// Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <opencv2/opencv.hpp>
#include <chrono>
#include <iomanip>
#include <iostream>
#include <ostream>
#include <vector>

#include <cstring>
#include <fstream>
#include <numeric>

#include "preprocess_op.h"

namespace ai {

void Permute::Run(const cv::Mat *im, float *data) {
  int rh = im->rows;
  int rw = im->cols;
  int rc = im->channels();
  for (int i = 0; i < rc; ++i) {
    cv::extractChannel(*im, cv::Mat(rh, rw, CV_32FC1, data + i * rh * rw), i);
  }
}

void Normalize0::Run(cv::Mat *im, const std::vector<float> &mean,
                    const std::vector<float> &scale, const bool is_scale) {
  double e = 1.0;
  if (is_scale) {
    e /= 255.0;
  }
  (*im).convertTo(*im, CV_32FC3, e);
  for (int h = 0; h < im->rows; h++) {
    for (int w = 0; w < im->cols; w++) {
      im->at<cv::Vec3f>(h, w)[0] =
          (im->at<cv::Vec3f>(h, w)[0] - mean[0]) * scale[0];
      im->at<cv::Vec3f>(h, w)[1] =
          (im->at<cv::Vec3f>(h, w)[1] - mean[1]) * scale[1];
      im->at<cv::Vec3f>(h, w)[2] =
          (im->at<cv::Vec3f>(h, w)[2] - mean[2]) * scale[2];
    }
  }
}

void ResizeImgType0::Run(const cv::Mat &img, cv::Mat &resize_img,
                         int max_size_len, float &ratio_h, float &ratio_w) {
  int w = img.cols;
  int h = img.rows;

  float ratio = 1.f;
  int max_wh = w >= h ? w : h;
  if (max_wh > max_size_len) {
    if (h > w) {
      ratio = float(max_size_len) / float(h);
    } else {
      ratio = float(max_size_len) / float(w);
    }
  }

  int resize_h = int(float(h) * ratio);
  int resize_w = int(float(w) * ratio);
  if (resize_h % 32 == 0)
    resize_h = resize_h;
  else if (resize_h / 32 < 1 + 1e-5)
    resize_h = 32;
  else
    resize_h = (resize_h / 32 - 1) * 32;

  if (resize_w % 32 == 0)
    resize_w = resize_w;
  else if (resize_w / 32 < 1)
    resize_w = 32;
  else
    resize_w = (resize_w / 32 - 1) * 32;

  cv::resize(img, resize_img, cv::Size(resize_w, resize_h));

  ratio_h = float(resize_h) / float(h);
  ratio_w = float(resize_w) / float(w);
}

void CrnnResizeImg::Run(const cv::Mat &img, cv::Mat &resize_img, float wh_ratio,
                        const std::vector<int> &rec_image_shape) {
  int imgC, imgH, imgW;
  imgC = rec_image_shape[0];
  imgH = rec_image_shape[1];
  // imgW = rec_image_shape[2];

  imgW = int(32 * wh_ratio);

  int resize_w, resize_h;
  if (ceilf(imgH * wh_ratio) > imgW)
    resize_w = imgW;
  else
    resize_w = int(ceilf(imgH * wh_ratio));

  cv::resize(img, resize_img, cv::Size(resize_w, imgH), 0.f, 0.f,
             cv::INTER_LINEAR);
}


void padresize_center(cv::Mat src,int net_w,int net_h,cv::Mat& dst)
{
    int origin_height,origin_width,height,width;
    origin_height=src.rows;
    origin_width=src.cols;
    width=net_w;
    height=net_h;
    printf("origin_height:%d origin_width:%d width:%d height:%d\n",origin_height,origin_width,width,height);
    cv::Mat M;
    float r = origin_height * 1.0 / origin_width;
    float r2=height*1.0/width;
    printf("r:%.4f r2:%.4f\n",r,r2);

    cv::Mat crop_img;
    cv::Mat imgPadResize=cv::Mat::zeros(height,width,CV_8UC3);

    if(r > r2)
    {
        float scale_r = origin_height/(float)height;
        int scale_w = (int)(origin_width/scale_r);
        int scale_h=height;
        int pad_w=width-scale_w;
        int crop_w=pad_w/2;
        int crop_h=0;
        printf("scale_w:%d scale_h:%d \n",scale_w,scale_h);
        cv::resize(src,crop_img,cv::Size(scale_w,scale_h),cv::INTER_NEAREST);
        crop_img.copyTo(imgPadResize(cv::Rect(crop_w,crop_h,scale_w,scale_h)));


    }
    else
    {
        float scale_r = origin_width/(float)width;
        int scale_h = (int)(origin_height/scale_r);
        int scale_w =width;
        int pad_h=height-scale_h;
        int crop_h=pad_h/2;
        int crop_w=0;

        printf("scale_w:%d scale_h:%d \n",scale_w,scale_h);
        cv::resize(src,crop_img,cv::Size(scale_w,scale_h),cv::INTER_NEAREST);
        crop_img.copyTo(imgPadResize(cv::Rect(crop_w,crop_h,scale_w,scale_h)));


    }
    dst=imgPadResize;
    
}

void PreProcessPlateReco(cv::Mat& img,int INPUT_W,int INPUT_H, int padding,cv::Mat& cropped)
{

    // int channel = 3;
    int input_w = INPUT_W;
    int input_h = INPUT_H;
    
    if (padding == 0)
    {    
        float scale = cv::min(float(input_w)/img.cols, float(input_h)/img.rows);
        auto scaleSize = cv::Size(img.cols * scale, img.rows * scale);

        cv::Mat resized;
        cv::resize(img, resized, scaleSize,0,0);

        cropped = cv::Mat::zeros(input_h, input_w, CV_8UC3);
        cv::Rect rect((input_w - scaleSize.width)/2, (input_h-scaleSize.height)/2, scaleSize.width, scaleSize.height);
        resized.copyTo(cropped(rect));
    }
    else if(padding == 1)
    {
        float scale = cv::min(float(input_w)/img.cols, float(input_h)/img.rows);
        auto scaleSize = cv::Size(img.cols * scale, img.rows * scale);

        cv::Mat resized;
        cv::resize(img, resized, scaleSize,0,0);

        cropped = cv::Mat::zeros(input_h, input_w, CV_8UC3);
        cv::Rect rect(0, (input_h-scaleSize.height)/2, scaleSize.width, scaleSize.height);
        resized.copyTo(cropped(rect));
    
    }
    else
    {
        auto scaleSize = cv::Size(input_w, input_h);
        cv::resize(img, cropped, scaleSize,0,0);
    }
    

}

} // namespace paddle_infer